//
//  STFileScreenController.m
//  smart_transferor
//
//  Created by wtc on 11-11-2.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "STFileScreenController.h"
#import "STFileItem.h"
#import "STAppDelegate.h"
#import "STFileViewController.h"
#import "STFileScreenProtocol.h"

 NSUInteger kNumberPerScreen = 16;

@implementation STFileScreenController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) initWithPage:(int)page In:(NSMutableArray *)array FromView:(STFileViewController *)fv App:(id<STFileScreenDelegate>)appd;
{
    if(array == nil)
    {
        return;
    }
    
    if(array.count <= page * kNumberPerScreen)
    {
        return;
    }
    
    filelist = array;
    fileview = fv;
    app = appd;
    
    int i;
    UIButton * btn;
    UILabel * lbl;
    
    for(i = 0; i < MIN(kNumberPerScreen, (array.count - page*kNumberPerScreen));i++)
    {
        CGRect frame;
        int index = i+page*kNumberPerScreen;
        
        btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        [btn setImage:((STFileItem *)[filelist objectAtIndex:index]).icon  forState:UIControlStateNormal];//设置按钮图片
        
        btn.tag = index;
        
        frame.size.width = 60;//设置按钮坐标及大小
        frame.size.height = 60;
        frame.origin.x = (i%4)*(60+10)+25;
        frame.origin.y = (i/4)*(60+16+3)+20;
        [btn setFrame:frame];
        
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [btn release];
        
        frame.size.width = 60;//设置按钮坐标及大小
        frame.size.height = 16;
        frame.origin.x = (i%4)*(60+10)+25;
        frame.origin.y = (i/4)*(60+16+3)+20+60;
        lbl = [[UILabel alloc] initWithFrame:frame];
        NSString * filename = ((STFileItem *)[filelist objectAtIndex:index]).fileName;

        lbl.text = filename;
        lbl.textColor = [UIColor whiteColor];
        lbl.textAlignment = UITextAlignmentCenter;
        lbl.backgroundColor = [UIColor clearColor];
        lbl.font = [UIFont systemFontOfSize:14.0];
        [self.view addSubview:lbl];
        [lbl release];
    }
    
}

-(void)btnPressed:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    int index = btn.tag;
    
    STFileItem * item = (STFileItem *)[filelist objectAtIndex:index];
    //[item composeAbsPathByDir:fileview.absDir AndName:item.fileName];
    if(app != nil)
    {
        [app selectFile:item FromFileView:fileview];
    }
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
